package com.we.scrm.web.api;


import com.we.scrm.common.enums.StatusEnum;
import com.we.scrm.common.page.Pagination;
import com.we.scrm.common.util.JsonView;
import com.we.scrm.domain.Category;
import com.we.scrm.domain.Customer;
import com.we.scrm.domain.Product;
import com.we.scrm.web.AbstractController;
import com.we.scrm.web.api.vo.UserVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/api")
public class MAppApiController extends AbstractController {

    @PostMapping("/getRecommend")
    @ResponseBody
    public String getRecommend() {
        Product queryEntity = new Product();
        queryEntity.setRecommend(StatusEnum.ENABLE.getCode());
        queryEntity.setStatus(StatusEnum.ENABLE.getCode());
        List<Product> recommendList = this.productService.queryAll(queryEntity);
        return JsonView.successJson(recommendList);
    }

    @PostMapping("/queryProducts")
    @ResponseBody
    public String queryProducts(Product queryEntity, Pagination<Product> page) {
        queryEntity.setStatus(StatusEnum.ENABLE.getCode());
        queryEntity.setRecommend(StatusEnum.DISABLE.getCode());
        if (null != queryEntity.getCategory() && 0 == queryEntity.getCategory()) {
            queryEntity.setCategory(null);
        }
        page = productService.queryPage(queryEntity, page);
        return JsonView.successJson(page.getItems());
    }
    @PostMapping("/queryCategory")
    @ResponseBody
    public String queryCategory(){
        List<Category> categoryList = this.categoryService.queryAll(new Category());
        return JsonView.successJson(categoryList);
    }
    @PostMapping("/getProduct")
    @ResponseBody
    public String getProduct(Product queryEntity){
        if(null != queryEntity.getId()){
            return JsonView.successJson(this.productService.getById(queryEntity.getId()));
        }
        return JsonView.failureJson("不存在该ID对应的商品");
    }
    @PostMapping("/getUserInfo")
    @ResponseBody
    public String getUserInfo(UserVO user){
        if(!user.valid()){
            JsonView.validJson("session key valid fail");
        }
        Customer customer = this.customerService.getByMOpenid(user.getOpenid());
        return JsonView.successJson(customer);
    }
}